package com.empire.module.member.controller.admin.mappolygonlog;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.mappolygonlog.vo.*;
import com.empire.module.member.dal.dataobject.mappolygonlog.MapPolygonLogDO;
import com.empire.module.member.service.mappolygonlog.MapPolygonLogService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 多边形操作日志")
@RestController
@RequestMapping("/member/map-polygon-log")
@Validated
public class MapPolygonLogController {

    @Resource
    private MapPolygonLogService mapPolygonLogService;

    @PostMapping("/create")
    @Operation(summary = "创建多边形操作日志")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-log:create')")
    public CommonResult<Long> createMapPolygonLog(@Valid @RequestBody MapPolygonLogSaveReqVO createReqVO) {
        return success(mapPolygonLogService.createMapPolygonLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新多边形操作日志")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-log:update')")
    public CommonResult<Boolean> updateMapPolygonLog(@Valid @RequestBody MapPolygonLogSaveReqVO updateReqVO) {
        mapPolygonLogService.updateMapPolygonLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除多边形操作日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:map-polygon-log:delete')")
    public CommonResult<Boolean> deleteMapPolygonLog(@RequestParam("id") Long id) {
        mapPolygonLogService.deleteMapPolygonLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得多边形操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-log:query')")
    public CommonResult<MapPolygonLogRespVO> getMapPolygonLog(@RequestParam("id") Long id) {
        MapPolygonLogDO mapPolygonLog = mapPolygonLogService.getMapPolygonLog(id);
        return success(BeanUtils.toBean(mapPolygonLog, MapPolygonLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得多边形操作日志分页")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-log:query')")
    public CommonResult<PageResult<MapPolygonLogRespVO>> getMapPolygonLogPage(@Valid MapPolygonLogPageReqVO pageReqVO) {
        PageResult<MapPolygonLogDO> pageResult = mapPolygonLogService.getMapPolygonLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MapPolygonLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出多边形操作日志 Excel")
    @PreAuthorize("@ss.hasPermission('member:map-polygon-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMapPolygonLogExcel(@Valid MapPolygonLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MapPolygonLogDO> list = mapPolygonLogService.getMapPolygonLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "多边形操作日志.xls", "数据", MapPolygonLogRespVO.class,
                        BeanUtils.toBean(list, MapPolygonLogRespVO.class));
    }

}